package com.lelouch.controller;

import com.google.common.collect.Maps;
import com.lelouch.bean.SysAcl;
import com.lelouch.bean.SysUser;
import com.lelouch.beans.PageQuery;
import com.lelouch.beans.PageResult;
import com.lelouch.common.JsonData;
import com.lelouch.param.AclParam;
import com.lelouch.param.UserParam;
import com.lelouch.service.SysAclService;
import com.lelouch.service.SysRoleAclService;
import com.lelouch.service.SysRoleUserService;
import com.lelouch.service.SysUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

/**
 * 权限点控制器
 *
 * @author lcy
 * @create 2018-06-19 17:16
 */
@Controller
@RequestMapping("/sys/acl")
@Slf4j
public class SysAclController {

    @Autowired
    private SysAclService sysAclService;
    @Autowired
    private SysRoleAclService sysRoleAclService;
    @Autowired
    private SysRoleUserService sysRoleUserService;
    /**
     * 新增权限点信息
     *
     * @param
     * @return
     */
    @RequestMapping("/save.json")
    @ResponseBody
    public JsonData save(AclParam aclParam) {
        sysAclService.save(aclParam);
        return JsonData.success("新添权限点成功");
    }

    /**
     * 更新权限点信息
     *
     * @param
     * @return
     */
    @RequestMapping("/update.json")
    @ResponseBody
    public JsonData update(AclParam aclParam) {
        sysAclService.update(aclParam);
        return JsonData.success("更新权限点成功");
    }


    @RequestMapping("/page.json")
    @ResponseBody
    public JsonData pageByAclModuleId(@RequestParam("aclModuleId") Integer aclModuleId, PageQuery pageQuery) {
        PageResult<SysAcl> pageResult = sysAclService.pageByAclModuleId(aclModuleId,pageQuery);
        return JsonData.success(pageResult);
    }



    @RequestMapping("/acls.json")
    @ResponseBody
    public JsonData acls(@RequestParam("aclId") Integer aclId) {
        Map map = Maps.newHashMap();
        map.put("roles",sysRoleAclService.getRoleListByAclId(aclId));
        map.put("users",sysRoleUserService.getUserListByAclId(aclId));
        return JsonData.success(map);
    }
}
